package com.ckerchina.admin.ckadmin.service.wx.messagehandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ckerchina.admin.ckadmin.dao.WxUserDAO;
import com.ckerchina.admin.ckadmin.domain.WXUser;
import com.ckerchina.admin.ckadmin.service.ZhuziWxService;
import com.ckerchina.admin.ckadmin.service.wx.WxEventMessage;
import com.ckerchina.admin.ckadmin.service.wx.WxMessage;
import com.ckerchina.admin.ckadmin.service.wx.WxMessageHandler;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 晓丰 on 2016/1/11.
 */
@Service
public class EventMessageHandler implements WxMessageHandler{

    @Autowired
    ZhuziWxService wxService;

    @Autowired
    WxUserDAO wxUserDAO;

    @Autowired
    List<WxEventHandler> wxEventHandlers;

    HashMap<String,WxEventHandler> eventHandlerMap;

    @PostConstruct
    public void init(){
    		eventHandlerMap = new HashMap<String,WxEventHandler>();
        for(WxEventHandler handler:wxEventHandlers){
            eventHandlerMap.put(handler.getEvent(),handler);
        }
    }

    @Override
    public WxMessage handleMessage(WxMessage wxMessage) throws Exception {
        if(!(wxMessage instanceof WxEventMessage)){
            return null;
        }
        WxEventMessage eventMessage= (WxEventMessage) wxMessage;
        String event = eventMessage.getEvent();
        WxEventHandler handler = eventHandlerMap.get(event);
        if(handler!=null){
            return handler.handleEvent(eventMessage);
        }
        return null;
    }



}
